package View;

import Bussiness.Insert_Action;
import Bussiness.Search_Action;
import Model.KhoaHoc;
import Model.LopHoc;

import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Toolkit;
import java.awt.SystemColor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 
 * @author Nhóm java lớp PNV2T - Nguyễn Thị Lệ Đài - Nguyễn Chí Hướng
 * 
 */
public class ThemLopHoc extends javax.swing.JFrame {
	Vector vecKhoaHoc = null;
	int courseId = 0;

	/** Creates new form ThemLopHoc */
	public ThemLopHoc(int courseId) {
		setAlwaysOnTop(true);
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				ThemLopHoc.class.getResource("/Icon/QLSV.jpg")));
		this.courseId = courseId;
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (UnsupportedLookAndFeelException ex) {
			ex.printStackTrace();
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
		} catch (InstantiationException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		initComponents();
		setLocation(520, 30);
		AddComBoBox();
	}

	public void AddComBoBox() {
		Search_Action sa = new Search_Action();
		vecKhoaHoc = sa.DanhSachKhoaHoc();
		cbb_KhoaHoc.removeAllItems();
		cbb_KhoaHoc.addItem("(--- Chọn Khóa Học ---)");
		for (int i = 0; i < vecKhoaHoc.size(); i++) {
			KhoaHoc kh = (KhoaHoc) vecKhoaHoc.get(i);
			cbb_KhoaHoc.addItem(kh.getTenKhoa());
		}
		sa.Close();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		jToolBar1 = new javax.swing.JToolBar();
		btn_CapNhap = new javax.swing.JButton();
		jSeparator1 = new javax.swing.JSeparator();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		cbb_HeDaoTao = new javax.swing.JComboBox();
		txt_TenLop = new javax.swing.JTextField();
		cbb_KhoaHoc = new javax.swing.JComboBox();
		txt_ChuNhiem = new javax.swing.JTextField();
		txt_LopTruong = new javax.swing.JTextField();
		jLabel8 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		jLabel10 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Th\u00eam  m\u1ed9t l\u1edbp h\u1ecdc m\u1edbi");
		setResizable(false);
		jPanel1.setBackground(SystemColor.textHighlight);
		jToolBar1.setBackground(SystemColor.inactiveCaption);
		jToolBar1.setFloatable(false);

		btn_CapNhap.setBackground(new java.awt.Color(227, 239, 249));
		btn_CapNhap.setFont(new java.awt.Font("Times New Roman", 1, 13));
		btn_CapNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Icon/Sua.png")));
		btn_CapNhap.setText("C\u1eadp Nh\u1eadp");
		btn_CapNhap.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btn_CapNhapActionPerformed(evt);
				QuanLy.Add_DanhSachLopHocChiTiet(1, courseId, "aaa");
			}
		});

		jToolBar1.add(btn_CapNhap);

		jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel3.setText("T\u00ean L\u1edbp :");

		jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel4.setText("Kh\u00f3a H\u1ecdc :");

		jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel5.setText("H\u1ec7 \u0110\u00e0o T\u1ea1o :");

		jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel6.setText("Ch\u1ee7 Nhi\u1ec7m :");

		jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel7.setText("L\u1edbp Tr\u01b0\u1edfng :");

		cbb_HeDaoTao.setFont(new java.awt.Font("Times New Roman", 0, 13));
		cbb_HeDaoTao.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] {
						"(--- Ch\u1ecdn H\u1ec7 \u0110\u00e0o T\u1ea1o ---)",
						"\u0110\u1ea1i H\u1ecdc Ch\u00ednh Qui",
						"Cao \u0110\u1eb3ng", "T\u1ea1i Ch\u1ee9c" }));

		txt_TenLop.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_TenLop.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		cbb_KhoaHoc.setFont(new java.awt.Font("Times New Roman", 0, 13));

		txt_ChuNhiem.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_ChuNhiem.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		txt_LopTruong.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_LopTruong.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		jLabel8.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel8.setForeground(new java.awt.Color(204, 0, 0));
		jLabel8.setText("(*)");

		jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel9.setForeground(new java.awt.Color(204, 0, 0));
		jLabel9.setText("(*)");

		jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel10.setForeground(new java.awt.Color(204, 0, 0));
		jLabel10.setText("(*)");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel7)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				txt_LopTruong,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				191,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel5)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								jLabel6))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								txt_ChuNhiem,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								191,
																								Short.MAX_VALUE)
																						.addComponent(
																								txt_TenLop,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								191,
																								Short.MAX_VALUE)
																						.addComponent(
																								cbb_KhoaHoc,
																								0,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								cbb_HeDaoTao,
																								0,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel10)
																						.addComponent(
																								jLabel9)
																						.addComponent(
																								jLabel8))))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
						.addComponent(jSeparator1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 304,
								Short.MAX_VALUE)
						.addComponent(jToolBar1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 304,
								Short.MAX_VALUE));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(
												jToolBar1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												38,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(8, 8, 8)
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(29, 29, 29)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																txt_TenLop,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel8))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(
																cbb_KhoaHoc,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel9))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																cbb_HeDaoTao,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel10))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel6)
														.addComponent(
																txt_ChuNhiem,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel7)
														.addComponent(
																txt_LopTruong,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));
		pack();
	}// </editor-fold>//GEN-END:initComponents


	private void btn_CapNhapActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btn_CapNhapActionPerformed
		String TenLop = txt_TenLop.getText();
		int row = cbb_KhoaHoc.getSelectedIndex();
		int index = cbb_HeDaoTao.getSelectedIndex();
		String ChuNhiem = txt_ChuNhiem.getText();
		String LopTruong = txt_LopTruong.getText();

		if (TenLop.equalsIgnoreCase("")) {
			JOptionPane.showMessageDialog(null,
					"B\u1ea1n ch\u01b0a nh\u1eadp T\u00ean L\u1edbp !",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
			txt_TenLop.requestFocus();
		} else if (row == 0) {
			JOptionPane.showMessageDialog(null,
					"B\u1ea1n ch\u01b0a ch\u1ecdn Kh\u00f3a H\u1ecdc !",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		} else if (index == 0) {
			JOptionPane
					.showMessageDialog(
							null,
							"B\u1ea1n ch\u01b0a ch\u1ecdn H\u1ec7 \u0110\u00e0o T\u1ea1o !",
							"Null Values !", JOptionPane.ERROR_MESSAGE);
		} else {
			LopHoc lop = new LopHoc();
			lop.setTenLop(TenLop);
			KhoaHoc kh = (KhoaHoc) vecKhoaHoc.get(row - 1);
			lop.setMaKhoa(kh.getID());
			if (index == 1)
				lop.setHeDaoTao("DH");
			else if (index == 2)
				lop.setHeDaoTao("CD");
			else if (index == 3)
				lop.setHeDaoTao("TC");
			lop.setChuNhiem(ChuNhiem);
			lop.setLopTruong(LopTruong);

			Insert_Action ia = new Insert_Action();
			if (ia.ThemLopHocMoi(lop) == true) {
				int j = JOptionPane
						.showConfirmDialog(
								null,
								"Th\u00eam th\u00e0nh c\u00f4ng ! B\u1ea1n c\u00f3 mu\u1ed1n th\u00eam ti\u1ebfp kh\u00f4ng ?",
								"Continue ... !", 2);
				if (j != 0)
					this.dispose();
				else {
					txt_ChuNhiem.setText("");
					txt_TenLop.setText("");
					txt_LopTruong.setText("");
					cbb_HeDaoTao.setSelectedIndex(0);
					cbb_KhoaHoc.setSelectedIndex(0);
				}
			} else {
				JOptionPane.showMessageDialog(this, "Khong them duoc !");
			}
		}
	}// GEN-LAST:event_btn_CapNhapActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton btn_CapNhap;
	private javax.swing.JComboBox cbb_HeDaoTao;
	private javax.swing.JComboBox cbb_KhoaHoc;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JToolBar jToolBar1;
	private javax.swing.JTextField txt_ChuNhiem;
	private javax.swing.JTextField txt_LopTruong;
	private javax.swing.JTextField txt_TenLop;
	// End of variables declaration//GEN-END:variables

}
